package com.hitqz.robot.biz.controller;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.StrUtil;
import com.hitqz.robot.api.business.dto.NetworkMappingDto;
import com.hitqz.robot.common.core.util.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 网络映射管理表
 *
 * @author robot
 * @date 2024-06-06 15:18:09
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/network")
public class NetworkMappingController {

    //table并添加元素
    private final Map<String, Map<String, String>> mapping = new HashMap<>(8);

    @PostConstruct
    private void init() {
        //wh
        HashMap<String, String> whExternalIpMapping = new HashMap<>();
        whExternalIpMapping.put("ws://192.168.3.55:8567/live?source=rtsp://admin:hbyt137258@192.168.100.164:554/streaming/channels/101", "ws://10.18.240.1:9999/admin/websocket/live?source=rtsp://admin:hbyt137258@192.168.3.55:16454/streaming/channels/101");
        whExternalIpMapping.put("ws://192.168.3.55:8567/live?source=rtsp://admin:hbyt137258@192.168.100.165:554/streaming/channels/101", "ws://10.18.240.1:9999/admin/websocket/live?source=rtsp://admin:hbyt137258@192.168.3.55:16554/streaming/channels/101");
        whExternalIpMapping.put("ws://192.168.3.45:8567/live?source=rtsp://admin:hbyt137258@192.168.100.164:554/streaming/channels/101", "ws://10.18.240.1:9999/admin/websocket/live?source=rtsp://admin:hbyt137258@192.168.3.45:16454/streaming/channels/101");
        whExternalIpMapping.put("ws://192.168.3.45:8567/live?source=rtsp://admin:hbyt137258@192.168.100.165:554/streaming/channels/101", "ws://10.18.240.1:9999/admin/websocket/live?source=rtsp://admin:hbyt137258@192.168.3.45:16554/streaming/channels/101");
        whExternalIpMapping.put("ws://192.168.3.35:8567/live?source=rtsp://admin:hbyt137258@192.168.100.164:554/streaming/channels/101", "ws://10.18.240.1:9999/admin/websocket/live?source=rtsp://admin:hbyt137258@192.168.3.35:16454/streaming/channels/101");
        whExternalIpMapping.put("ws://192.168.3.35:8567/live?source=rtsp://admin:hbyt137258@192.168.100.165:554/streaming/channels/101", "ws://10.18.240.1:9999/admin/websocket/live?source=rtsp://admin:hbyt137258@192.168.3.35:16554/streaming/channels/101");
        whExternalIpMapping.put("ws://192.168.3.25:8567/live?source=rtsp://admin:hbyt137258@192.168.100.164:554/streaming/channels/101", "ws://10.18.240.1:9999/admin/websocket/live?source=rtsp://admin:hbyt137258@192.168.3.25:16454/streaming/channels/101");
        whExternalIpMapping.put("ws://192.168.3.25:8567/live?source=rtsp://admin:hbyt137258@192.168.100.165:554/streaming/channels/101", "ws://10.18.240.1:9999/admin/websocket/live?source=rtsp://admin:hbyt137258@192.168.3.25:16554/streaming/channels/101");
        whExternalIpMapping.put("ws://192.168.3.10:8568/live?source=rtsp://admin:hit159357@192.168.3.78:554/streaming/channels/101", "ws://10.18.240.1:9999/admin/websocket/live?source=rtsp://admin:hit159357@192.168.3.78:554/streaming/channels/101");
        whExternalIpMapping.put("ws://192.168.3.10:8568/live?source=rtsp://admin:hit159357@192.168.3.78:554/streaming/channels/201", "ws://10.18.240.1:9999/admin/websocket/live?source=rtsp://admin:hit159357@192.168.3.78:554/streaming/channels/201");
        //mapping.put("192.168.16.12", externalIpMapping);
        mapping.put("10.18.240.1", whExternalIpMapping);
        //zq
        mapping.put("192.168.3.25", Map.of("192.168.100.164:80", "192.168.3.25:164","192.168.3.25:80", "192.168.3.25:164"));
        mapping.put("192.168.3.35", Map.of("192.168.100.164:80", "192.168.3.35:164","192.168.3.35:80", "192.168.3.35:164"));
        mapping.put("192.168.100.100", Map.of("192.168.3.35:80", "192.168.100.164:80","192.168.3.25:80", "192.168.100.164:80"));
    }


    @Operation(summary = "请求网络映射结果")
    @PostMapping(value = "mapping")
    public R<NetworkMappingDto> mapping(@RequestBody NetworkMappingDto dto) {
        String target = dto.getTarget();
        String host = dto.getHost();
        dto.setMapping(target);
            if (StrUtil.isBlank(host) || Objects.equals("localhost", host) || Objects.equals("127.0.0.1", host)) {
                return R.ok(dto);
            }
            if (mapping.containsKey(host)) {
                Map<String, String> targetMapping = mapping.get(host);
                if (targetMapping.containsKey(target)) {
                    dto.setMapping(targetMapping.get(target));
                }
            }
        return R.ok(dto);
    }


    @Operation(summary = "批量请求网络映射结果")
    @PostMapping(value = "ListMapping")
    public R<List<NetworkMappingDto>> ListMapping(@RequestBody List<NetworkMappingDto> dtoList) {
        for (NetworkMappingDto dto : dtoList) {
            String host = dto.getHost();
            String target = dto.getTarget();
            dto.setMapping(target);
            if (StrUtil.isBlank(host) || Objects.equals("localhost", host) || Objects.equals("127.0.0.1", host)) {
                continue;
            }
            if (mapping.containsKey(host)) {
                Map<String, String> targetMapping = mapping.get(host);
                if (targetMapping.containsKey(target)) {
                    dto.setMapping(targetMapping.get(target));
                }
            }
        }
        return R.ok(dtoList);
    }


    public static void main(String[] args) {
        //base64 encode
        String encode = Base64.encode("ws://192.168.3.55:8567/live?source=rtsp://admin:hbyt137258@192.168.100.164:554/streaming/channels/101");
        System.out.println(encode);
        System.out.println(Base64.decodeStr(encode));
    }


}